package com.code;

/**
 * 归并排序
 * Created by lucky on 2018/9/21.
 */
public class GB_Sort {

    public static int[] gbSort(int[] A , int q){
        int r = A.length;
        int p = r - q;
        int[] L = new int[q+1];
        int[] R = new int[r-q+1];
        for (int i = 0 ; i < q ; i++){
            L[i] = A[i];
        }
        for (int j = 0 ; j < p ; j ++){
            R[j] = A[p+j];
        }
        int m = 0;
        int n = 0;
        L[q] = 777;
        R[p] = 777;
        for(int k = 0 ; k < r ; k++){
            if(L[m] <= R[n]){
                A[k] = L[m];
                m++;
            }else{
                A[k] = R[n];
                n++;
            }
        }
        return A;
    }

    public static void main(String[] args) {
        int[] B = {4,6,8,10,1,2,3,7};
        int[] ints = gbSort(B, 4);
        for (int i = 0 ; i<ints.length;i++){
            System.out.print(ints[i] + ",");
        }
    }
}
